<template>
  <div class="grid-container">
    <div class="detai-box" v-show="isNull">
      <el-empty description="暂无数据"></el-empty>
    </div>
    <div class="grid-box" v-show="!isNull">
      <div
        class="grid-items"
        v-for="(item, index) in moreItem"
        :key="index"
        :style="{
          width: 'calc(100' + '%/' + gridShowNum + ')',
          aspectRatio: aspectRatio,
        }"
      >
        <div class="item-container flex-col">
          <div class="grid-item">
            <div class="grid-img-box">
              <div class="popups">
                <el-button type="text" @click="open(item)" class="button">
                  {{ item.itemstate == 1 ? "取消寄售" : "寄售" }}
                </el-button>

                <div class="button" @click="toInfo(item)">查看</div>
                <div class="button" @click="giveOpen(item)">转赠</div>
              </div>
              <el-image
                class="grid-img"
                :src="item.url"
                :fit="cover"
              ></el-image>
            </div>
            <div class="grid-text"></div>
          </div>
          <div class="state flex">
            <p class="text-gray-400 text-sm">{{ item.name }}</p>
            <p class="text-sm">编号：{{ item.numbering }}</p>
          </div>
          <div class="state flex">
            <p class="text-gray-400 text-sm">藏品状态</p>
            <p class="text-sm">{{ item.itemstate == 1 ? "已寄售" : "持有" }}</p>
          </div>
          <div class="price"></div>
        </div>
      </div>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="sellForm"
        :rules="rules"
        :model="sellform"
        label-width="0px"
      >
        <el-form-item label="" prop="">
          <span>价格</span>
          <el-input v-model="sellform.sellPrice"></el-input>
        </el-form-item>
        <el-form-item label="" prop="payPassword">
          <span>支付密码</span>
          <el-input v-model="sellform.payPassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSell('sellFrom')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 转增表单 -->
    <el-dialog
      title="提示"
      :visible.sync="giveDialogVisible"
      width="30%"
      :before-close="giveHandleClose"
    >
      <el-form
        ref="giveForm"
        :rules="rules"
        :model="giveForm"
        label-width="0px"
      >
        <el-form-item label="" prop="username">
          <span>受赠送用户</span>
          <el-input v-model="giveForm.username"></el-input>
        </el-form-item>
        <el-form-item label="" prop="payPassword">
          <span>支付密码</span>
          <el-input v-model="giveForm.payPassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="giveDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="give(updateitem)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "MineNftGrid",
  props: {
    gridShowNum: {
      type: Number,
      default: 0,
    },
    aspectRatio: {
      type: String,
      default: "none",
    },
    type: {
      typeof: String,
      default: "itemGroups",
    },
    search: {
      typeof: String,
      default: "",
    },
    sort: {
      typeof: String,
      default: "time",
    },
  },
  data() {
    //交易密码判断
    var sellPasswordCheck = (rule, value, callback) => {
      if (value === "") {
        console.log("密码为空");
        callback(new Error("密码不能为空"));
      } else {
        this.$taxios
          .postInfo("/checkoutTPassword", { pwd: value })
          .then((res) => {
            console.log(res);
            if (res.state == 1) {
              callback();
            }
            callback("交易密码错误");
          });
      }
    };

    var sellPriceCheck = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("价格不能为空"));
      } else if (value > 20000) {
        callback(new Error("价格不能大于2w"));
      }
    };
    // 判断用户是否存在规则
    var usernameExit = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("用户名不能为空"));
      } else {
        this.$axios
          .postInfo("/existUserInfoUsername", {
            username: this.giveForm.username,
          })
          .then((res) => {
            console.log(res);
            if (res.state == false) {
              callback(new Error("用户不存在"));
              return new Promise((resolve, reject) => {});
            } else {
              return this.$taxios.postInfo("/getUserInfo", {
                token: this.$cookies.get("token"),
              });
            }
          })
          .then((res) => {
            if (this.giveForm.username === res.userInfo.username) {
              console.log(res);
              callback(new Error("不能选自己"));
            } else {
              callback();
            }
          });
      }
    };
    return {
      isNull: true,
      aspectRatios: "none",
      moreItem: [],
      items: [],
      cover: "cover",
      //寄售弹窗
      dialogVisible: false,
      updateitem: "",
      sellform: {
        sellPrice: "",
        payPassword: "",
      },
      // 转增弹窗
      giveDialogVisible: false,
      giveForm: {
        username: "",
        payPassword: "",
      },
      rules: {
        sellPrice: [
          { validator: sellPriceCheck, required: true, trigger: "blur" },
        ],
        payPassword: [
          { validator: sellPasswordCheck, required: true, trigger: "blur" },
        ],
        username: [
          { validator: usernameExit, required: true, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    updateItemGroups(data) {
      var items = this.$axios.postInfo("/itemGroup", { id: null });
      items
        .then((res) => {
          this.moreItem = res.itemGroups
            .slice(0)
            .reverse()
            .sort((a, b) => {
              return b.sort - a.sort;
            });
          console.log(res.items);
          console.log(this.moreItem);
        })
        .then((res) => {});
    },
    updateItems() {
      let query = {};
      console.log(this.sort);
      this.$set(query, this.sort, this.sort);
      this.$set(query, "itemState", "1");
      this.$taxios.postInfo("/myItems", query).then((res) => {
        let itemGroup = res.itemGroup;
        let items = res.items;
        let arr = [];
        items.forEach((item) => {
          if (item.name.indexOf(this.search) >= 0) {
            arr.push(item);
          }
        });
        this.moreItem = arr.slice(0);
        if (this.moreItem.length > 0) {
          this.isNull = false;
        }
      });
    },

    toInfo(item) {
      console.log(item);
      this.$router.push({
        path: "/item",
        query: { group: item.group, numbering: item.numbering },
      });
    },
    //是否取消寄售
    open(value) {
      if (value.itemstate == 1) {
        this.$confirm("是否取消寄售", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          beforeClose: (action, instance, done) => {
            if (action == "confirm") {
              let state = this.cancelSell(value);
              console.log(state);
              this.$message.promptBox("取消寄售成功", "success");
              location.reload();
              done();
            } else if (action == "cancel") {
              console.log(action);
              done();
            }
          },
        })
          .then(() => {
            this.$message.promptBox("取消寄售成功", "success");
          })
          .catch(() => {
            this.$message.promptBox("已取消操作", "success");
          });
      } else {
        this.updateitem = value;
        this.dialogVisible = true;
      }
    },
    //取消寄售
    cancelSell(value) {
      console.log(value);
      this.updateitem=value;
      this.$axios
        .updateItemInfo("/updateItemState", {
          itemState: 0,
          group: this.updateitem.group,
          numBerIng: this.updateitem.numbering,
        })
        .then((res) => {
          console.log(res);
          this.dialogVisible = false;
        });
    },
    // 寄售表单验证
    toSell(value) {
      console.log(this.$refs["sellForm"].validate);
      console.log(this.sellform.sellPrice);
      this.$refs["sellForm"].validate((valid) => {
        if (valid) {
          this.$axios
            .updateItemInfo("/updateItemState", {
              itemState: 1,
              price: this.sellform.sellPrice,
              group: this.updateitem.group,
              numBerIng: this.updateitem.numbering,
            })
            .then((res) => {
              console.log(res);
              this.dialogVisible = false;
              this.$message.promptBox("上架成功", "success");
              location.reload();
            });
        } else {
          console.log(valid);
          console.log("error.submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.$refs["sellForm"].resetFields();
          done();
        })
        .catch((_) => {});
    },

    //转增
    giveOpen(value) {
      if (value.itemstate == 1) {
        this.$message.promptBox("藏品还在寄售", "warning");
      } else {
        this.giveDialogVisible = true;
        this.updateitem = value;
      }
    },
    //转增表单验证
    give(value) {
      console.log(this.updateitem);
      this.$refs["giveForm"].validate((valid) => {
        if (valid) {
          this.$axios
            .updateItemInfo("/giveItem", {
              username: this.giveForm.username,
              group: value.group,
              Numbering: value.numbering,
            })
            .then((res) => {
              console.log(res);
              if (res.state == 1) {
                this.$message.promptBox("转增成功", "success");
              } else {
                this.$message.promptBox("转增失败", "error");
              }
              location.reload();
            });
        } else {
          return false;
        }
      });
    },
    giveHandleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          // this.$refs["sellForm"].resetFields();
          done();
        })
        .catch((_) => {});
    },

    open4() {
      this.$message.promptBox("取消寄售成功", "success");
    },
  },
  created() {
    console.log(this.type);
    if (this.type == "itemGroups") {
      console.log(this.type);
      this.updateItemGroups();
    } else if (this.type == "items") {
      this.updateItems();
    }
  },
  watch: {
    sort(val) {
      this.updateItems();
    },
    search() {
      this.updateItems();
    },
  },
};
</script>
<style  lang="less" scoped>
.grid-container {
  position: relative;
  width: 100%;
  max-width: 1200px;
  // display: flex;
  align-items: center;
  // justify-content: center;
  margin-top: 20px;
  margin-bottom: 50px;
  padding: 0% 2rem;

  .grid-box {
    position: relative;
    width: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;

    .grid-items {
      height: auto;
      padding: 10px 5px 10px 5px;
      box-sizing: border-box;
      display: flex;
      transition: 0.6s;

      .item-container {
        display: flex;
        justify-content: center;
        width: 100%;
        height: 100%;
        box-shadow: 4px 4px 6px rgb(143, 140, 140);
        @apply rounded-lg;
        padding: 0.3rem 0.3rem;
        overflow: hidden;

        .grid-item {
          position: relative;
          width: 100%;
          height: 100%;
          // background: #2465;
          box-sizing: border-box;
          overflow: hidden;
          background: #fff;
          display: flex;
          flex-direction: column;
          justify-content: flex-end;
          @apply rounded-lg;

          .grid-img-box {
            position: absolute;
            width: 100%;
            height: 100%;
            overflow: hidden;

            .grid-img {
              position: relative;
              height: 100%;
              width: 100%;
              overflow: hidden;

              /deep/ .el-image__inner {
                transition: 0.6s;
                filter: blur(20px);
                -webkit-filter: blur(0px);
              }
            }

            /deep/ .el-image__inner {
              transition: 0.6s;
              filter: blur(20px);
              -webkit-filter: blur(0px);
            }

            .popups {
              position: absolute;
              top: 0%;
              left: 0%;
              right: 0;
              bottom: 0%;
              margin: auto;
              width: 100%;
              height: 100%;
              background: rgba(22, 21, 21, 0.757);
              border: 2px solid #fff;
              border-radius: 20px;
              line-height: 40px;
              z-index: 2;
              display: block;
              @apply rounded-lg;
              display: none;
              flex-direction: column;
              justify-content: center;
              align-items: center;

              .button {
                position: relative;
                width: 80%;
                height: 15%;
                max-height: 30px;
                display: flex;
                align-items: center;
                justify-content: center;
                background: #fbfbfb51;
                color: #fff;
                @apply rounded-3xl;
                margin-top: 30px;
                cursor: pointer;
              }

              .el-button--text {
                color: #fff;
              }
            }
          }

          .grid-img-box:hover {
            .popups {
              display: flex;
            }
          }

          .grid-text {
            position: relative;
            height: auto;
            width: 100%;
            // top: 80%;
            background: white;
            // padding-bottom: 1rem;
            display: flex;
            flex-direction: column;

            .name {
              position: relative;
              width: 100%;

              h1 {
                text-align: left;
                padding: 0.5rem;
                padding-bottom: 0%;
                font-size: 1.2rem;
                // box-sizing: border-box;
                // color: rgb(16, 209, 209);
                font-weight: 600;
                text-indent: 0.5rem;
              }
            }

            .price-box {
              position: relative;
              flex: 1;
              width: 100%;
              height: 60%;
              display: flex;
              align-items: center;
              justify-content: space-between;
              padding: 0.3rem 1rem;
              box-sizing: border-box;

              img {
                height: 100%;
                width: 35px;
              }

              .price {
                position: relaative;
                right: 0%;
                width: 80px;
                // padding: 5px 10px 5px 10px;
                padding: 0.2rem 0.5rem;
                box-sizing: border-box;
                background: black;
                color: white;
                display: flex;
                align-items: center;
                justify-content: center;
                opacity: 0.3;
                cursor: pointer;
                @apply rounded-lg;
              }

              .price:hover {
                opacity: 0.8;
              }
            }
          }
        }

        .state {
          padding: 0.5rem 0.4rem;
          justify-content: space-between;
        }
      }
    }
  }

  .el-dialog__wrapper {
    /deep/ .el-dialog {
      width: 400px;

      .el-form-item {
        text-align: left;

        span {
          // text-align: left;
        }
      }

      .el-input__icon {
        width: auto;
      }

      .el-dialog__footer {
      }
    }
  }

  .dialog-footer {
    .el-button {
      color: black;
    }

    .el-button:hover {
      background: #409eff;
    }
  }
}
</style>